package org.example.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.MySqlDialect;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import org.apache.ibatis.executor.Executor;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/6/7 21:51
 */
public class MyPaginationInnerInterceptor extends PaginationInnerInterceptor {

    /**
     * 自定义方言 解决不能识别数据库类型的分页问题
     * @param executor Executor
     * @return
     */
    @Override
    protected IDialect findIDialect(Executor executor) {
        if (getDialect() != null) {
            return getDialect();
        }
        if (getDbType() != null) {
            return DialectFactory.getDialect(getDbType());
        }
        DbType dbType = JdbcUtils.getDbType(executor);
        if (dbType != null) {
            return DialectFactory.getDialect(dbType);
        }
        // 不能解析 jdbc:bytehouse: 当作 clickhouse 处理，，返回 MySqlDialect
        return new MySqlDialect();
    }
}
